//
//  HomeDetailView.swift
//  maosheng
//
//  Created by lxl on 2019/8/20.
//

import UIKit
import Kingfisher
class HomeDetailView: PublicSwiftTableView {
    var blogId : String?
    let commentIdentifer = "DetailCommentCell"
    override func addTheRefresh() {
        tableView.register(DetailCommentCell.self, forCellReuseIdentifier: commentIdentifer)

        addFooterRefreshFinish { [weak self] in
           self?.getTheDetailCommentList(isHeader: false)
        }
    }
    func setTheDetailInfo(model : HomeDetialModel){
   

        self.str_Title = model.title != nil ?  model.title : ""
        if let user = model.creatorUser{
            self.str_userName = user.nickName
            self.str_headline = user.headLine
            if let imgUrl = user.avatar{
                self.avaterImg.kf.setImage(with: URL(string: imgUrl), placeholder: UIImage(named: "login_head_moren"))
            }
        }
        self.addTheHeaderView()
        self.str_desc = model.body
        tableView.reloadData()
//        tableView.reloadData()

    }
    //获取评论
    func getTheDetailCommentList(isHeader : Bool) -> Void {
        if blogId == nil{
            return
        }
        if isHeader{
            self.pageIndex = 1
        }
//        http://maosheng.csproj.com/api/services/app/Comment/GetCommentPagingListAsync? CommentedObjectId=57&Sorting=Id desc&ModuleTypeId=100301
        let str_url = "api/services/app/Comment/GetCommentPagingListAsync"
        var params = [String : Any]()
        params["CommentedObjectId"] = blogId
        params["Sorting"] = "Id desc"
        params["ModuleTypeId"] = 100301
        params["PageIndex"] = pageIndex
        params["PageSize"] = 30

        SDataCenter.shared.SGetJsonDataWithUrl(str_url: str_url, params: params) { [weak self] (responseData, isSuccess) in
            if self?.tableView.mj_header != nil{
                self?.tableView.mj_header.endRefreshing()
                self?.tableView.mj_footer.endRefreshing()
            }
            
            if isSuccess && responseData is [String : Any]{
                if isHeader{
                    self?.dataSource.removeAllObjects()
                }
                if let items = (responseData as! [String : Any])["items"]{
                    if let arr = items as? Array<Any>{
                        if let attentionArr = [HomeCommentModel].deserialize(from: arr){
                            self?.dataSource.addObjects(from: attentionArr as [Any])
                        }
                    }
                    
                }
                if self?.dataSource.count == 0{
                    ProgressHUD.showMsgWithoutView("暂无评论")
                }
                //                self?.checkNeedNoDataView()
                self?.setRefreshFooter()
            }else{
                if !isHeader{
                    self?.pageIndex = self!.pageIndex - 1
                }
                
            }
        }
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: commentIdentifer) as! DetailCommentCell
        let model = self.dataSource[indexPath.row]
         cell.setTHeCommentInfo(model: model as! HomeCommentModel)
        cell.selectionStyle = .none
        return cell
    }
    
   
}
